package com.s7.compat.status.compat;

import android.annotation.TargetApi;
import android.os.Build;
//import android.support.design.widget.AppBarLayout;
//import android.support.design.widget.CollapsingToolbarLayout;
//import android.support.design.widget.CoordinatorLayout;
//import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;

import androidx.appcompat.widget.Toolbar;
import androidx.coordinatorlayout.widget.CoordinatorLayout;

import com.google.android.material.appbar.AppBarLayout;
import com.google.android.material.appbar.CollapsingToolbarLayout;
import com.s7.compat.status.ColorManager;
import com.s7.compat.status.StatusBarManager;
import com.s7.compat.status.impl.StatusBar;
import com.s7.compat.status.widget.StatusBarView;

import java.lang.ref.WeakReference;
import java.math.BigDecimal;

public class KitkatCompat implements StatusBar {

    private static final String STATUS_BAR_VIEW_TAG = "ghStatusBarView";

    @TargetApi(Build.VERSION_CODES.KITKAT)
    @Override
    public void setStatusBarColor(Window window, int color) {
        window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        ViewGroup decorViewGroup = (ViewGroup) window.getDecorView();
        View statusBarView = decorViewGroup.findViewWithTag(STATUS_BAR_VIEW_TAG);
        if (statusBarView == null) {
            statusBarView = new StatusBarView(window.getContext());
            statusBarView.setTag(STATUS_BAR_VIEW_TAG);
            FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
                    FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT);
            params.gravity = Gravity.TOP;
            statusBarView.setLayoutParams(params);
            decorViewGroup.addView(statusBarView);
        }
        statusBarView.setBackgroundColor(color);
        StatusBarManager.internalSetFitsSystemWindows(window, true);
    }


    @TargetApi(Build.VERSION_CODES.KITKAT)
    @Override
    public void translucentStatusBar(Window window, boolean translucent) {
    }

    @Override
    public void setToolbarColor(Window window, View toolbar, int color, int alpha) {
        toolbar.setBackgroundColor(ColorManager.toColorAlpha(color, alpha));
    }

    @TargetApi(Build.VERSION_CODES.KITKAT)
    @Override
    public void setCollapsingToolbar(Window window, AppBarLayout bar, final CollapsingToolbarLayout collapsing,
                                     final Toolbar toolbar, final int color) {
        ((View) bar.getParent()).setFitsSystemWindows(false);
        toolbar.setBackgroundColor(ColorManager.toColorAlpha(color, 0f));
        bar.setFitsSystemWindows(false);
        toolbar.setFitsSystemWindows(false);
        CoordinatorLayout.Behavior behavior = ((CoordinatorLayout.LayoutParams) bar.getLayoutParams()).getBehavior();
        if (behavior != null && behavior instanceof AppBarLayout.Behavior) {
            int verticalOffset = ((AppBarLayout.Behavior) behavior).getTopAndBottomOffset();
            if (Math.abs(verticalOffset) > collapsing.getScrimVisibleHeightTrigger()) {
                toolbar.setBackgroundColor(ColorManager.toColorAlpha(color, 1f));
            } else {
                toolbar.setBackgroundColor(ColorManager.toColorAlpha(color, 0f));
            }
        } else {
            toolbar.setBackgroundColor(ColorManager.toColorAlpha(color, 0f));
        }
        collapsing.setFitsSystemWindows(false);
        final WeakReference<Window> weakReference = new WeakReference<>(window);
        bar.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
            @Override
            public void onOffsetChanged(AppBarLayout barLayout, int verticalOffset) {
                Window weakWindow = weakReference.get();
                if (weakWindow != null) {
                    int height = barLayout.getHeight() - collapsing.getScrimVisibleHeightTrigger();
                    float transparent = new BigDecimal(Math.abs(verticalOffset))
                            .divide(new BigDecimal(height), 2, BigDecimal.ROUND_HALF_UP).floatValue();
                    if (Math.abs(verticalOffset) >= height) {
                        toolbar.setBackgroundColor(ColorManager.toColorAlpha(color, 1f));
                    } else {
                        if (Math.abs(verticalOffset) > 0) {
                            toolbar.setBackgroundColor(ColorManager.toColorAlpha(color, transparent));
                        } else {
                            toolbar.setBackgroundColor(ColorManager.toColorAlpha(color, 0f));
                        }
                    }
                }
            }
        });
        collapsing.getChildAt(0).setFitsSystemWindows(false);
        collapsing.setStatusBarScrimColor(color);
    }
}
